package MapPack;

/**
 * Created by Aaron on 8/5/2014.
 */

import EntityPack.Entity;

import java.util.ArrayList;

public class Cell {
    private final Map map;
    private Map.CellEnum cellType;
    private ArrayList<Entity> entities = new ArrayList<Entity>();
    private final int x;
    private final int y;
    private final int elevation;
    private boolean passable;

    public Cell(Map map, int x, int y, int elevation) {
        this(map, x, y, elevation, Map.CellEnum.MARTIANSOIL, true);
    }

    public Cell(Map map, int x, int y, int elevation, Map.CellEnum tileType, boolean passable) {
        this.map = map;
        this.x = x;
        this.y = y;
        this.elevation = elevation;
        this.cellType = tileType;
        this.passable = passable;
    }

    public Map.CellEnum getCellType ( ) {
        return this.cellType;
    }

    public void setCellType (Map.CellEnum cellType) {
        this.cellType = cellType;
        switch (cellType) {
            default:
                this.passable = true;
        }
    }

    public ArrayList<Entity> getEntities ( ) {
        return this.entities;
    }

    public void addEntity (Entity entity) {
        this.entities.add(entity);
    }

    public boolean removeEntity (Entity entity) {
        return this.entities.remove(entity);
    }

    public int getX ( ) {
        return this.x;
    }

    public int getY ( ) {
        return this.y;
    }

    public int getElevation ( ) {
        return this.elevation;
    }

    public boolean isPassable ( ) {
        return this.passable;
    }

    public boolean isWithinDistance (Cell cell, int maxDistance) {
        return (Math.abs(this.x - cell.getX())
                + Math.abs(this.y - cell.getY())
                <= maxDistance);
    }

    public boolean isWithinDistance (Entity entity, int maxDistance) {
        return isWithinDistance(entity.getPosition(), maxDistance);
    }

    public ArrayList<Cell> findCellsWithinDistance (int maxDistance) {

        ArrayList<Cell> result = new ArrayList<>();
        Cell cell;

        for (int x = this.x; x <= maxDistance; x++) {
            for (int y = this.y; y <= maxDistance; y++) {
                cell = this.map.getMapCell(x, y);
                if (isWithinDistance(cell, maxDistance)) {
                    result.add(cell);
                }
            }
        }

        return result;
    }
}